﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moon.Sql
{
    internal class DataMapperFactory
    {
        public static Func<IDataReader, object> Builder(IDataReader reader, Type type)
        {
            if (type == typeof(object) || type == typeof(DynamicRow))
            {
                return DataMapperDynamicImpl.Builder(reader, type, null);
            }
            Type underlyingType = null;
            if (!(DataMapper.TypeMap.ContainsKey(type) || type.IsEnum || type.FullName == "System.Data.Linq.Binary"
                || (type.IsValueType && (underlyingType = Nullable.GetUnderlyingType(type)) != null && underlyingType.IsEnum)))
            {
                return DataMapperEntityImpl.Builder(reader, type, null);
            }
            return DataMapperStructImpl.Builder(reader, type, underlyingType ?? type);
        }
    }
}
